package com.game.modular.game.battle.process.skillEffectHandle.base;

import com.game.comment.exection.InfoTips;
import com.game.comment.util.BeanUtil;
import com.game.modular.game.battle.panel.skillEffect.PvpSkillEffect;
import com.game.modular.game.battle.panel.skillEffect.domain.BaseEffectValueInfo;
import com.game.modular.game.battle.panel.skillEffect.domain.SkillEffectLayer;
import com.game.modular.game.battle.panel.attribute.domain.PlayerHeroAttribute;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * 锁定 基础
 * 触发值|衰弱值
 */
@Slf4j
@Component
public class EffectHandle1 {

    /**
     * 锁定 基础
     * 触发值|衰弱值
     *
     * @param pvpSkillEffect 技能效果
     */
    public void tryInitProperties(PvpSkillEffect pvpSkillEffect) {
        Integer status = pvpSkillEffect.getStatus().getStatus();
        if (status == 1) {
            tryObtainNumericalValues(pvpSkillEffect);
            tryBaseChangeValue(pvpSkillEffect);
        }
    }

    /**
     * 1-1
     * 基础触发值
     */
    public void tryObtainNumericalValues(PvpSkillEffect pvpSkillEffect) {
        SkillEffectLayer skillEffectLayer = pvpSkillEffect.getSkillEffectLayer();
        // 校验
        if (skillEffectLayer.getName() == null) {
            log.error("changeField.name is null 数值操作失败");
            throw new InfoTips("changeField.name is null");
        }
        String targetInfo = BeanUtil.getTargetInfo(new PlayerHeroAttribute(), skillEffectLayer.getName());
        skillEffectLayer.setInfo(targetInfo);
        pvpSkillEffect.setSkillEffectLayer(skillEffectLayer);

        BaseEffectValueInfo baseEffectValueInfo = pvpSkillEffect.getBaseEffectValueInfo();
        double changeValue = getChangeValue(baseEffectValueInfo);
        baseEffectValueInfo.setTriggerValue(changeValue);
        pvpSkillEffect.setBaseEffectValueInfo(baseEffectValueInfo);
    }

    /**
     * 1-2
     * 基础衰弱值
     */
    public void tryBaseChangeValue(PvpSkillEffect pvpSkillEffect) {
        BaseEffectValueInfo baseEffectValueInfo = pvpSkillEffect.getBaseEffectValueInfo();
        double decayValue = -tryWeakValue(pvpSkillEffect);
        baseEffectValueInfo.setDecayValue(decayValue);
        pvpSkillEffect.setBaseEffectValueInfo(baseEffectValueInfo);
    }

    /**
     * 1-3
     * 计算弱化数值
     */
    public Double tryWeakValue(PvpSkillEffect pvpSkillEffect) {
        BaseEffectValueInfo baseEffectValueInfo = pvpSkillEffect.getBaseEffectValueInfo();
        Double resDoubleValue2 = baseEffectValueInfo.getResDoubleValue2();
        Double resIntegerValue2 = baseEffectValueInfo.getResIntegerValue2();
        boolean b1 = resDoubleValue2 != null;
        boolean b2 = resIntegerValue2 != null;

        double weakValue = 0.0;
        if (b2 || b1) {
            BaseEffectValueInfo clone = baseEffectValueInfo.clone();
            clone.setResDoubleValue1(resDoubleValue2);
            clone.setResIntegerValue1(resIntegerValue2);
            weakValue = getChangeValue(clone);
        }
        return weakValue;
    }


    /**
     * 获取加成值
     */
    private double getAddChangeValue(BaseEffectValueInfo baseEffectValueInfo) {
        double back = 0.0;
        Double resDoubleValue1 = baseEffectValueInfo.getResDoubleValue1();
        if (resDoubleValue1 != null) {
            back += baseEffectValueInfo.getBaseValue() * resDoubleValue1;
        }
        Double resIntegerValue1 = baseEffectValueInfo.getResIntegerValue1();
        if (resIntegerValue1 != null) {
            back += resIntegerValue1;
        }
        return back;
    }

    private double getReduceChangeValue(BaseEffectValueInfo baseEffectValueInfo) {
        return -getAddChangeValue(baseEffectValueInfo);
    }

    double getChangeValue(BaseEffectValueInfo baseEffectValueInfo) {
        Integer isBenefit = baseEffectValueInfo.getIsBenefit();
        switch (isBenefit) {
            case 1:
                return getAddChangeValue(baseEffectValueInfo);
            case 0:
                return getReduceChangeValue(baseEffectValueInfo);
            default:
                return 0;
        }
    }
}
